<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_laporan_pembelian_produk extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_laporan_pembelian_produk_model', 'IspPembelianP');
		//$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Laporan Pembelian Produk";	
		viewByTemplate('isp_laporan_pembelian_produk/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspPembelianP->GetJSON();
	}
	function cetak(){
		$as_id_pembelian= get('id_pembelian');
		
		if($as_id_pembelian!='' && $as_id_pembelian!='')
		{
			
			// Load FPDF
			$this->load->helper('fpdf');
			
				$data['data']= $this->IspPembelianP->GetCetak($as_id_pembelian);

			// Load View
			$this->load->view('isp_laporan_pembelian_produk/cetak', $data);
			
		}
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_customer") == '') {
			$error->Add("kode_customer", "Kode customer", $tpl_empty);
		
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
			    
				$data	=  array(
						'kode_customer' => $this->input->post('kode_customer'),
						//'nama_customer' => $this->input->post('nama_customer'),
						//'nama_perusahaan' => $this->input->post('nama_perusahaan'),
						'kode_produk' => $this->input->post('kode_inventori'),
						'nama_produk' => $this->input->post('nama_inventori'),
						'stock' => $this->input->post('banyak'),
						'harga_satuan' => $this->input->post('harga'),
						'jumlah' => $this->input->post('jumlah'),
						'total_bayar' => $this->input->post('total_bayar'),
						'id_pembelian' => $this->input->post('id_pembelian')
					);


				
				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->IspPembelianP->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					
					$this->IspPembelianP->Update($data, array('id_pembelian'	=> $data['id_pembelian']));

				$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$ls_response = "";
		$no_id 	= get('id_pembelian');
		
		if ($no_id != "")
		{
			// Delete Single
			$arr_rule	= array('id_pembelian' => $id_pembelian);
			$ls_result = $this->IspPembelianP->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_pembelian' => $row['id_pembelian']);
				$ls_result .= $this->IspPembelianP->DeleteSingle($arr_rule);
				$ls_response = "Data Terhapus";
			}
		}
		
		echo $ls_result;
	}
}
?>